{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 最长连续序列\n",
    "\n",
    "[![Author](https://img.shields.io/badge/Author-%40mjd507-important?style=social&logo=GitHub)](https://github.com/mjd507)\n",
    "[![WeChat Channel](https://img.shields.io/badge/Wechat-Daily%20Problem-important?style=social&logo=WeChat)](https://mp.weixin.qq.com/s/94wN8vFhCJV2wfcLVf9-bA)\n",
    "\n",
    "![Logo](img/Day86-1.png)\n",
    "\n",
    "&emsp;&emsp;给定一个整数数组，返回数组中最长连续子序列的长度。\n",
    "\n",
    "&emsp;&emsp;例如，输入数组`[100, 4, 200, 1, 3, 2]`拥有最长连续子序列`[1, 2, 3, 4]`。因此，你需要返回数值`4`。\n",
    "\n",
    "&emsp;&emsp;你能在$O(n)$时间复杂度内完成吗？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def longest_consecutive(arr: list) -> int:\n",
    "    dictionary = {}\n",
    "    for val in arr:\n",
    "        dictionary[val] = val\n",
    "    maximum = 1\n",
    "    currMax = 1\n",
    "    prevVal = None\n",
    "    for key in sorted(list(dictionary.keys())):\n",
    "        if prevVal is not None:\n",
    "            if key - prevVal == 1:\n",
    "                currMax += 1\n",
    "                maximum = max(maximum, currMax)\n",
    "            else:\n",
    "                currMax = 1\n",
    "        prevVal = key\n",
    "    return maximum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "print(longest_consecutive([100, 4, 200, 1, 3, 2]))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
